﻿#region Namespace References

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DynConDevCorp.OpenArch.SmartData.WinForms.ControlBinders;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.WinForms.ControlBinders
{
    /// <summary>
    /// 
    /// </summary>
    public class ControlBinderFactory
    {
        #region Constructors

        private ControlBinderFactory()
        {
            Register(typeof (Control), typeof (SmartTextBoxBinder));
            Register(typeof (TextBox), typeof (SmartTextBoxBinder));
            Register(typeof (Label), typeof (SmartLabelBinder));
            Register(typeof (ComboBox), typeof (SmartComboBoxBinder));
        }

        #endregion

        #region Public Static Properties

        /// <summary>
        /// Gets the global.
        /// </summary>
        /// <value>The global.</value>
        public static ControlBinderFactory Global
        {
            get { return s_Global; }
        }

        #endregion

        #region Public Instance Properties

        /// <summary>
        /// Gets the bindable control types.
        /// </summary>
        /// <value>The bindable control types.</value>
        public Type[] BindableControlTypes
        {
            get
            {
                List<Type> retVal = new List<Type>();
                retVal.AddRange(m_RegisteredBinderTypes.Keys);
                return retVal.ToArray();
            }
        }

        #endregion

        #region Public Instance Methods

        /// <summary>
        /// Creates the specified control type.
        /// </summary>
        /// <param name="controlType">Type of the control.</param>
        /// <returns></returns>
        public SmartControlBinder Create(Type controlType)
        {
            Type binderType;
            SmartControlBinder binder;
            if (m_RegisteredBinderTypes.TryGetValue(controlType, out binderType))
            {
                binder = (SmartControlBinder) Activator.CreateInstance(binderType);
            }
            else
            {
                binder = null;
            }
            return binder;
        }

        /// <summary>
        /// Creates the specified control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <returns></returns>
        public SmartControlBinder Create(Control control)
        {
            Type controlType = control.GetType();
            SmartControlBinder binder = Create(controlType);
            if (binder != null)
            {
                binder.BoundControl = control;
            }
            return binder;
        }

        /// <summary>
        /// Registers the specified control type.
        /// </summary>
        /// <param name="controlType">Type of the control.</param>
        /// <param name="controlBinderType">Type of the control binder.</param>
        public void Register(Type controlType, Type controlBinderType)
        {
            m_RegisteredBinderTypes.Add(controlType, controlBinderType);
        }

        #endregion

        #region Fields and Constants

        private static readonly ControlBinderFactory s_Global = new ControlBinderFactory();

        private readonly Dictionary<Type, Type> m_RegisteredBinderTypes = new Dictionary<Type, Type>();

        #endregion
    }
}